<template>
  <v-form v-model="valid" ref="myArticledForm">
    <v-text-field v-model="articled.title" label="请输入标题" required :rules="titleRules"/>
    <v-text-field v-model="articled.description" label="请输入描述" />
    <v-cascader
      url="/info/category/list"
      multiple
      required
      v-model="articled.categories"
      label="请选择资讯分类"/>
    <v-layout row>
      <v-flex xs3>
        <span style="font-size: 16px; color: #444">上传图片：</span>
      </v-flex>
      <v-flex>
        <v-upload
          v-model="articled.imagePath"  url="/upload/image" :multiple="false" :pic-width="250" :pic-height="90"
        />
      </v-flex>
    </v-layout>
    <v-layout class="my-4" row>
      <v-flex>
        <v-editor v-model="articled.content" upload-url="/upload/image"/>
      </v-flex>
    </v-layout>
    <v-layout class="my-4" row>
      <v-spacer/>
      <v-btn @click="submit" color="primary">提交</v-btn>
      <v-btn @click="clear">重置</v-btn>
    </v-layout>
  </v-form>
</template>

<script>
  export default {
    name: "articled-form",
    props: {
      oldArticle: {
        type: Object
      },
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        valid: false, // 表单校验结果标记
        articled: {
          title:'',
          description:'',
          imagePath:'',
          content:'',
          categories: [], // 资讯所属商品分类数组
        },
        titleRules: [
          v => !!v || "标题不能为空",
          v => v.length > 1 || "标题至少2位"
        ]
      }
    },
    methods: {
      submit() {
        // 表单校验
        if (this.$refs.myArticledForm.validate()) {
          // 定义一个请求参数对象，通过解构表达式来获取articled中的属性
          const {title,description,categories,imagePath,content, ...params} = this.articled;
          // 数据库中只要保存分类的id即可，因此我们对categories的值进行处理,只保留id，并转为字符串
          params.categoryIds = categories.map(c => c.id).join(",");

          params.title = title;
          params.description = description;
          params.imagePath = imagePath;
          params.content = content;
          // 将字母都处理为大写
         // params.letter = letter.toUpperCase();
          // 将数据提交到后台
          // this.$http.post('/item/brand', this.$qs.stringify(params))
          this.$http({
            method: this.isEdit ? 'put' : 'post',
            url: '/info/article',
            data: params,//this.$qs.stringify(params)
          }).then(() => {
            // 关闭窗口
            this.$emit("close");
            this.$message.success("保存成功！");
          })
            .catch(() => {
              this.$message.error("保存失败！");
            });
        }
      },
      clear() {
        // 重置表单
        this.$refs.myArticledForm.reset();
        // 需要手动清空商品分类
        this.categories = [];
      }
    },
    watch: {
      oldArticle: {// 监控oldArticled的变化
        handler(val) {
          if (val) {
            // 注意不要直接复制，否则这边的修改会影响到父组件的数据，copy属性即可
            this.articled = Object.deepCopy(val)
          } else {
            // 为空，初始化brand
            this.articled = {
              title:'',
              description:'',
              imagePath:'',
              content:'',
              categories: [], // 资讯所属商品分类数组
            }
          }
        },
        deep: true
      }
    }
  }
</script>

<style scoped>

</style>
